package servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Board;
import model.FeedBack;
import DAO.BoardDao;
import DAO.FeedBackDao;

public class AnswerNewFeedBack implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		FeedBack feedBack = new FeedBack();
		
		FeedBackDao feedBackDao = new FeedBackDao();
		
		HttpSession session=request.getSession();
	
		ActionForward forward=new ActionForward();
		
			
		int studyId = request.getParameter("studyId") != null
				&& request.getParameter("studyId") != "" ? Integer
				.parseInt(request.getParameter("studyId")) : 0;
		int userId = session.getAttribute("userid") != null
				&& session.getAttribute("userid") != "" ? Integer
				.parseInt((String)session.getAttribute("userid")) : 0;

		int questId = request.getParameter("questId") != null
				&& request.getParameter("questId") != "" ? Integer
				.parseInt(request.getParameter("questId")) : 0;

		int answerId = request.getParameter("answerId") != null
				&& request.getParameter("answerId") != "" ? Integer
				.parseInt(request.getParameter("answerId")) : 0;
				
		if (userId < 1) {
			session.invalidate();
			Util.ShowError(response, "로그인해주세요");
		}
		if (studyId < 1 || questId < 1 || answerId < 1 ) {
			Util.ShowError(response, "잘못 된 접근입니다");
		}

		int targetId = request.getParameter("targetId")!= null && request.getParameter("targetId") 
				!= ""? Integer.parseInt(request.getParameter("targetId")) : null;	
		String content = request.getParameter("content")!= null && request.getParameter("content") 
				!= ""? request.getParameter("content") : null;	
				
		if(content==null)  {
			//error return
			Util.ShowError(response, "값을 올바로 입력해주세요");
			
			return null;
		}
	
		content=Util.toUTF8(content);

		feedBack.setQid(questId);
		feedBack.setAid(answerId);
		feedBack.setContent(content);
		feedBack.setUid(userId);
		feedBack.setTargetId(targetId);
		
		feedBackDao.insertFeedBackInfo(feedBack);
		
	
		// go to details studyId;
		forward.setPath("./Info.Answer");
		return forward;
	}

}
